package controller;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

import model.log;

/*****************************************
 * onLog
 * 
 * frogState for when the frog is on a log
 * 
 * @author Stephen Gregory, Jun Wang
 *
 */

public class onLog implements frogState {

	@Override
	public void handleInput(userInputController c, char keyinput) {
		//Note: need to create condition to check if entire word was typed, 
		//if so then change the velocity of the frog and set the state to jumping
		//c.setState(c.jumpingState);
		
		//or if the last word is typed, change state to crossedRiver
		//c.setState(c.crossedRiverState);
		if (keyinput == KeyEvent.VK_ESCAPE){
			c.model.setActiveLog(null);
			c.model.setNumLettersTyped(0);
		}
		else if (c.model.getActiveLog() == null) {
			ArrayList<log> list = c.model.getLogs();
			
			//for the list of logs, compare the first letter with input
			for (int i = 0; i < list.size(); i++) {
				//checks if keyinput matches and log is in first row
				if ((c.model.getFrog().getPosY() - list.get(i).getPosY() < 110) 
						&& (c.model.getFrog().getPosY() - list.get(i).getPosY() > 0) 
						&& (list.get(i).getWord().charAt(0) == keyinput)) {
					c.model.setActiveLog(list.get(i));
					c.model.setActiveLogIndex(i);
					c.model.setNumLettersTyped(1);
				}
			}
		}
		else if (c.model.getActiveLog().getPosX()<900 && c.model.getActiveLog().getPosX()>0) {
			log activelog = c.model.getActiveLog();
			int letterTyped = c.model.getNumLettersTyped();
			
			//if the whole word is typed
			if (keyinput == activelog.getWord().charAt(letterTyped)
					&& letterTyped == activelog.getWord().length()-1){
				c.model.incCurScore();
				
				//check what type the active log is
				c.model.activeLogTyped();
				
				
				
				
				if (c.model.getActiveLogIndex() == 25) {
					//c.model.setFrogPosVel(activelog.getPosX()+80, activelog.getPosY()-20, activelog.getVelocityX());
					c.setState(c.crossedRiverState);
				}
				else {
					//c.model.setFrogPosVel(activelog.getPosX()+60, activelog.getPosY()-20, activelog.getVelocityX());
					c.setState(c.onLogState);
				}
			}
			if (keyinput == activelog.getWord().charAt(letterTyped)) {
				//increment
				letterTyped++;
				c.model.setNumLettersTyped(letterTyped);
			}
		}
	}

	@Override
	public void frogHitEdge(userInputController c) {
		c.model.lifeLost();
		
		if (c.model.isGameOver()) {
			c.setState(c.gameOverState);
		}
		else
			c.setState(c.onBankState);
		
	}
}
		

